﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Ultima.Xna.Input;

namespace Ultima.Xna.Graphics.UI.Gumps
{
    public enum GumpButtonType 
    {
		Page = 0,
		Reply = 1
    }

    public class GumpButton : GumpEntry
    {
        private int _normalId;
        private int _pressedId;
        private int _buttonId;
        private int _param;

        private bool _pressed;

        private Texture2D _normalTexture;
        private Texture2D _pressedTexture;

        public bool Pressed
        {
            get { return _pressed; }
        }

        public Texture2D NormalTexture
        {
            get { return _normalTexture; }
        }

        public Texture2D PressedTexture
        {
            get { return _pressedTexture; }
        }

        public int NormalId
        {
            get { return _normalId; }
            set { _normalId = value; }
        }

        public int PressedId
        {
            get { return _pressedId; }
            set { _pressedId = value; }
        }

        public int ButtonId
        {
            get { return _buttonId; }
            set { _buttonId = value; }
        }

        public int Param
        {
            get { return _param; }
            set { _param = value; }
        }

        public override Vector2 Size
        {
            get
            {
                if (_pressed)
                {
                    if (_pressedTexture != null)
                    {
                        return new Vector2(_pressedTexture.Width, _pressedTexture.Height);
                    }
                    else
                    {
                        return base.Size;
                    }
                }
                else
                {
                    if (_normalTexture != null)
                    {
                        return new Vector2(_normalTexture.Width, _normalTexture.Height);
                    }
                    else
                    {
                        return base.Size;
                    }
                }
            }
            set
            {
                base.Size = value;
            }
        }

        public GumpButton(Game game)
            : base(game)
        {
            Clickable = true;
        }

        public override void Initialize()
        {
            base.Initialize();

            _normalTexture = Data.Gumps.CreateTexture(Game.GraphicsDevice, _normalId);
            _pressedTexture = Data.Gumps.CreateTexture(Game.GraphicsDevice, _pressedId);
        }

        protected override void OnMouseDown(object sender, MouseEventArgs e)
        {
            _pressed = e.ContainsButtonDown(MouseButtons.LeftButton);

            base.OnMouseDown(sender, e);
        }

        protected override void OnMouseUp(object sender, MouseEventArgs e)
        {
            _pressed = !e.ContainsButtonDown(MouseButtons.LeftButton);

            base.OnMouseUp(sender, e);
        }

        protected override void OnMouseLeave(object sender, MouseEventArgs e)
        {
            _pressed = false;

            base.OnMouseLeave(sender, e);
        }

        public override void Draw(GameTime gameTime)
        {
            Manager.Renderer.Render(this);

            base.Draw(gameTime);
        }
    }
}
